if (!app.user) {app.redirect('/admin/login.php')}
if (!window.location.hash) {
	window.location.replace('product_list.php');
}

jQuery(function() {

	window.productId = window.location.hash.slice(1);

	function getProduct(id) {
		var serverReturn = _get('/app/index.php?d=admin&c=product&m=get_product', 
			{'product_id': id});
		return serverReturn.product;
	}

	function setProduct(id, sets) {
		var response = _post(
			'/app/index.php?d=admin&c=product&m=set_product&product_id='+id, 
			sets);
		return response.result || false;
	}

	function updateProductImg(product) {
		$('#product-high-discount-picture').attr('src', '/app/'+product['product_high_discount_picture']);
		$('#product-low-discount-picture').attr('src', '/app/'+product['product_low_discount_picture']);
		$('#product-sharing-picture').attr('src', '/app/'+product['product_sharing_picture']);
		$('#product-pre-vote-picture').attr('src', '/app/'+product['product_pre_vote_picture']);
	}

	function updateProductProperties(product) {
		$('#inputProductRequiredVotes').val(product['product_required_votes']);  // 预定投票数
		$('#inputProductVoteCount').val(product['product_vote_count']);  // 当前投票数
		$('#inputProductShareCount').val(product['product_share_count']);  // 当前投票数
		$('#inputProductHighDiscount').val(product['product_high_discount']*10);  // 终极折扣
		$('#inputProductLowDiscount').val(product['product_low_discount']*10);  // 心动折扣
		$('#inputProductHighDiscountLink').val(product['product_high_discount_link']);  // 终极链接
		$('#inputProductLowDiscountLink').val(product['product_low_discount_link']);  // 心动链接
		$('#inputProductFullPriceLink').val(product['product_full_price_link']);  // 原价链接
		$('#inputProductName').val(product['product_name']);  // 产品名称
		$('[action="restore-product-vote-count"]').html('恢复为实际人数('+product['product_real_vote_count']+')');
		$('[action="restore-product-share-count"]').html('恢复为实际人数('+product['product_real_share_count']+')');
	}
	

	var updateProduct = function() {
		window.productId = window.location.hash.slice(1);
		var product = getProduct(productId);
		updateProductImg(product);
		updateProductProperties(product);
	}

	updateProduct();
	window.onhashchange = updateProduct;

	$(document).delegate('[action="restore-product-vote-count"]', 'click', function() {
		var handle = _get(
			'/app/index.php?d=admin&c=product&m=restore_product_vote_count&product_id='+window.productId
		);
		if (!handle || typeof handle['error'] != 'undefined') {
			return showFailed(this, 1);
		}
		$('#inputProductVoteCount').val(handle['product_vote_count']);
		showSuccess(this);
	});

	$(document).delegate('[action="restore-product-share-count"]', 'click', function() {
		var handle = _get(
			'/app/index.php?d=admin&c=product&m=restore_product_share_count&product_id='+window.productId
		);
		if (!handle || typeof handle['error'] != 'undefined') {
			return showFailed(this, 1);
		}
		$('#inputProductShareCount').val(handle['product_share_count']);
		showSuccess(this);
	});

	$('form[name="product"] [type="submit"]').click(function(e) {
		e.preventDefault();
		var sets = {};
		$('form[name="product"] :input').not('button').each(function(i, ele) {
			sets[$(ele).attr('name')] = $(ele).val();
		})
		sets['product_high_discount'] = sets['product_high_discount']/10;
		sets['product_low_discount'] = sets['product_low_discount']/10;
		if (!setProduct(productId, sets)) {
			showFailed(this);
		}
		showSuccess(this);
	})

	$('#productHighDiscountPictureModal [action="submit"]').click(function(e){
		e.preventDefault();
		$.ajaxFileUpload ({
			url: '/app/index.php?d=admin&c=product&m=set_product_high_discount_picture&product_id='+productId,
			secureuri: false,
			fileElementId: 'inputProductHighDiscountPicture',
			dataType: 'json',
			success: function (data, status) {
				if(typeof(data.error) != 'undefined'){
					return alert(data.error);
				}
				$('#product-high-discount-picture').attr('src', data.picture)
				$('#productHighDiscountPictureModal [action="cancel"]').trigger("click");
			},
			error: function (data, status, e) {
				alert(e);
			}
		})
	});

	$('#productLowDiscountPictureModal [action="submit"]').click(function(e){
		e.preventDefault();
		$.ajaxFileUpload ({
			url: '/app/index.php?d=admin&c=product&m=set_product_low_discount_picture&product_id='+productId,
			secureuri: false,
			fileElementId: 'inputProductLowDiscountPicture',
			dataType: 'json',
			success: function (data, status) {
				if(typeof(data.error) != 'undefined'){
					return alert(data.error);
				}
				$('#product-low-discount-picture').attr('src', data.picture)
				$('#productLowDiscountPictureModal [action="cancel"]').trigger("click");
			},
			error: function (data, status, e) {
				alert(e);
			}
		})
	});

	$('#productSharingPictureModal [action="submit"]').click(function(e){
		e.preventDefault();
		$.ajaxFileUpload ({
			url: '/app/index.php?d=admin&c=product&m=set_product_sharing_picture&product_id='+productId,
			secureuri: false,
			fileElementId: 'inputProductSharingPicture',
			dataType: 'json',
			success: function (data, status) {
				if(typeof(data.error) != 'undefined'){
					return alert(data.error);
				}
				$('#product-sharing-picture').attr('src', data.picture)
				$('#productSharingPictureModal [action="cancel"]').trigger("click");
			},
			error: function (data, status, e) {
				alert(e);
			}
		})
	});

	$('#productPreVotePictureModal [action="submit"]').click(function(e){
		e.preventDefault();
		$.ajaxFileUpload ({
			url: '/app/index.php?d=admin&c=product&m=set_product_pre_vote_picture&product_id='+productId,
			secureuri: false,
			fileElementId: 'inputProductPreVotePicture',
			dataType: 'json',
			success: function (data, status) {
				if(typeof(data.error) != 'undefined'){
					return alert(data.error);
				}
				$('#product-pre-vote-picture').attr('src', data.picture)
				$('#productPreVotePictureModal [action="cancel"]').trigger("click");
			},
			error: function (data, status, e) {
				alert(e);
			}
		})
	});
})